package com.tjw.ec.common.core.util;

import com.tjw.ec.common.core.constant.HttpConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;


/**
 * 响应信息主体
 *
 * @param <T>
 * @author T-junwen
 */
@Builder
@ToString
@Accessors(chain = true)
@AllArgsConstructor
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;


    @Getter
    @Setter
    private T data;

    public Response() {
    }

    public Response(T data) {
        Response<Object> response = new Response<>();
        response.setMsg("success");
        response.setCode(HttpConstants.SUCCESS);
        response.setData(data);
    }

    public Response(T data, String msg) {
        Response<Object> response = new Response<>();
        response.setMsg(msg);
        response.setCode(HttpConstants.SUCCESS);
        response.setData(data);
    }

    public Response(Throwable e) {
        Response<Object> response = new Response<>();
        response.setMsg(e.getMessage());
        response.setCode(HttpConstants.FAIL);
        response.setData(data);
    }

    public static Response faile() {
        Response<Object> response = new Response<>();
        response.setCode(HttpConstants.FAIL);
        response.setMsg("请求失败!");
        return response;
    }
}
